package com.iotimc.attendancesystem.dao;

import com.github.yuweiguocn.library.greendao.MigrationHelper;
import com.iotimc.attendancesystem.App;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * @author david
 */
public class GreenDaoManager {
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private static volatile GreenDaoManager mInstance = null;

    private GreenDaoManager() {
        if (mInstance == null) {
            //重写MySQLiteOpenHelper数据库升级，数据不丢失
            //MyApplication.getContext()上下文表示了数据库存储路径为手机内存
            //DEBUG表示是否打印debug
            //一个是将sql语句打印出来
            QueryBuilder.LOG_SQL = true;
            //一个是将传入QueryBuilder的参数打印出来
            QueryBuilder.LOG_VALUES = true;

            MigrationHelper.DEBUG = true;
            SQLiteOpenHelper helper = new
                    SQLiteOpenHelper(App.getAppContext(), "user.db", null);
            mDaoMaster = new DaoMaster(helper.getWritableDatabase());
            mDaoSession = mDaoMaster.newSession();
        }
    }

    public static GreenDaoManager getInstance() {
        if (mInstance == null) {
            synchronized (GreenDaoManager.class) {
                if (mInstance == null) {
                    mInstance = new GreenDaoManager();
                }
            }
        }
        return mInstance;
    }

    public DaoMaster getMaster() {
        return mDaoMaster;
    }

    public DaoSession getSession() {
        return mDaoSession;
    }

    public DaoSession getNewSession() {
        mDaoSession = mDaoMaster.newSession();
        return mDaoSession;
    }
}